APM.ConfigServiceController = Ember.Controller.extend({
	init : function() {
		this._super();
		this.initTime();
		this.initConfData();
	},
	form : {
		machineName : "broada_apm",
		localIp : "192.168.1.1",
		subnetMask : "255.255.255.0",
		gateway : "",
		firstDNS : "",
		secondDNS : "",
		systemDate : new Date(new Date() - timeD_value),
		systemTime : "",
		autoSync : false,
		webPort : "80",
		apmServerEnable : true,
		remoteIp : "",
		port : "",
		timeServer : "0.pool.ntp.org",
		confType:"3"
	},
	
	initConfData: function(){
		var ctrl = this;
		var form = this.get("form");
		ctrl.store.ajax("GET", "configs/systemConfig",{
			configItem:"server"
		}).then(function(response){
			var configobj=response.configData;
			if(configobj){
				if(configobj.server_basic_gateway){
					ctrl.set("form.gateway",configobj.server_basic_gateway);
				}
				if(configobj.server_basic_host_name){
					 ctrl.set("form.machineName",configobj.server_basic_host_name);
				}
				if(configobj.server_time_auto_sync =="1"){
					 ctrl.set("form.autoSync",true);
				}else{
					 ctrl.set("form.autoSync",false);
				}
				if(configobj.server_basic_dns2){
					 ctrl.set("form.secondDNS",configobj.server_basic_dns2);
				}
				if(configobj.server_basic_dns1){
					 ctrl.set("form.firstDNS",configobj.server_basic_dns1);
				}
				if(configobj.server_basic_netmask){
					 ctrl.set("form.subnetMask",configobj.server_basic_netmask);
				}
				if(configobj.server_time_ntp_server){
					 ctrl.set("form.timeServer",configobj.server_time_ntp_server);
				}
				if(configobj.server_basic_ip){
					 ctrl.set("form.localIp",configobj.server_basic_ip);
				}
			}	
		});
		
		ctrl.store.ajax("GET", "configs/systemConfig",{
			confType:form.confType,
			configItem:"service"
		}).then(function(response){
			var configobj1=response.configData;
			if(configobj1){
				if(configobj1.service_indicator_enabled=="1"){
					 ctrl.set("form.apmServerEnable",true);
				}else{
					ctrl.set("form.apmServerEnable",false);
				}
				if(configobj1.service_web_port){
					ctrl.set("form.webPort",configobj1.service_web_port);
				}
				if(configobj1.service_indicator_remote_port){
					 ctrl.set("form.port",configobj1.service_indicator_remote_port);
				}
				if(configobj1.service_indicator_remote_ip){
					 ctrl.set("form.remoteIp",configobj1.service_indicator_remote_ip);
				}
			}	
		});
		
	},
	
	timeServerArray : [ {
		label : "0.pool.ntp.org",
		value : "0.pool.ntp.org"
	}, {
		label : "1.pool.ntp.org",
		value : "1.pool.ntp.org"
	}, {
		label : "2.poo.ntp.org",
		value : "2.poo.ntp.org"
	}, {
		label : "1.asia.pool.ntp.org",
		value : "1.asia.pool.ntp.org"
	}, {
		label : "timekeeper.isi.edu",
		value : "timekeeper.isi.edu"
	} ],
	
	inpDisabled : Ember.computed(function() {
		return true;
	}),

	// 禁用远程inp输入框
	apmServerEnableChange : Ember.observer("form.apmServerEnable", function() {
		var apmServerEnableVal = this.get("form.apmServerEnable");
		if (apmServerEnableVal) {
			this.set("inpDisabled", true);
		} else {
			this.set("inpDisabled", false);
		}
	}),
	
	initTime:function(){
		var ctrl = this;
		ctrl.store.ajax("GET", "configs/systemTime",{
			
		}).then(function(response){
			var curTime=response.nowtime;
			ctrl.set("localTime", curTime);
		});
		
		this.getDateInterval();
	},
	
	getDateInterval : function() {
		var ctrl = this;
		
		this.getDateTimer = setInterval(function(){
			var time=ctrl.get("localTime");
			var day1=time.substr(0,11);
			var day2=time.substr(22,time.length);
			var hms=time.substr(13,11).split(":");
			var hh=parseInt(hms[0]);
			var mm=parseInt(hms[1]);
			var ss=parseInt(hms[2]);
//			function timechange(hh,mm,ss){
			var hh1;
			var mm1;
			var ss1;
				 ss1=ss+1;
				if(ss1==60){
					mm1=mm+1;
					ss1=0;
				}
				if(mm1==60){
					hh1=hh+1;
					mm1=0;
				}else{
					mm1=mm;
					hh1=hh;
				}
				if(hh1==24){
					hh1=0;
				}
				
					if(hh1<10){
						hh1="0"+hh1.toString();
					}
					if(mm1<10){
						mm1="0"+mm1.toString();
					}
					if(ss1<10){
						ss1="0"+ss1.toString();
					}
					
				
				var ctime=day1+"  "+hh1+":"+mm1+":"+ss1+" "+day2;
				ctrl.set("localTime", ctime);
//			}
		},1000);
	},
	
	checkFormValid: function(){
		var form = this.get("form");
		this.set("saveResult", null);
		
		if(form.machineName){
		//验证由数字、26个英文字母或者下划线组成的字符串
			var reg0=new RegExp("^[0-9a-zA-Z_]{1,}$");
			if(!reg0.test(form.machineName)){
			 this.set("formTip", "对不起，您输入的主机名称含非法字符，请重新输入!");
			 return false;
			}
		}else{
			 this.set("formTip", "主机名不能为空!");
			 return false;
		}	
		if(form.localIp){
			var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg1.test(form.localIp)){  
				   this.set("formTip", "对不起，您输入的本机IP地址非法，请重新输入!");
				   return false;
			} 
		}else{
			 this.set("formTip", "本机IP地址不能为空!");
			 return false;
		}
		if(form.subnetMask){
			var reg2=/^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)$/;
			if(!reg2.test(form.subnetMask)){  
				   this.set("formTip", "对不起，您输入的子网掩码非法，请重新输入!");
				   return false;
			} 
		}else{
			 this.set("formTip", "子网掩码不能为空!");
			 return false;
		}
		if(form.gateway){
			var reg3=/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
			if(!reg3.test(form.gateway)){  
				   this.set("formTip", "对不起，您输入的网关非法，请重新输入!");
				   return false;
			} 
		}
		if(form.firstDNS){
			var reg4=/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
			if(!reg3.test(form.firstDNS)){  
				   this.set("formTip", "对不起，您输入的网关非法，请重新输入!");
				   return false;
			} 
		}
		if(form.secondDNS){
			var reg5=/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
			if(!reg5.test(form.secondDNS)){  
				   this.set("formTip", "对不起，您输入的网关非法，请重新输入!");
				   return false;
			} 
		}
		if(form.webPort){
			var reg = new RegExp("^[0-9]*$");
			var wport=parseInt(form.webPort);
			if(!(reg.test(form.webPort) && wport<65536)){  
				   this.set("formTip", "对不起，web端口错误，请重新输入!");
				   return false;
			} 
		}else{
			 this.set("formTip", "web端口不能为空!");
			   return false;
		}
		if(form.remoteIp){
			var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg1.test(form.remoteIp)){  
				   this.set("formTip", "对不起，您输入的远程指标计算服务地址非法，请重新输入!");
				   return false;
			} 
		}
		if(form.port){
			var reg = new RegExp("^[0-9]*$"); 
			var port1=parseInt(form.port);
			if(!(reg.test(form.port) && port1<65536)){  
				   this.set("formTip", "对不起，服务端口错误，请重新输入!");
				   return false;
			} 
		}
		
		this.set("formTip", null);
		return true;
	},
	
	actions : {
		reset : function() {
			clearInterval(this.getDateTimer);
			this.set("formTip", null);
			this.set("saveResult", null);
		},

		save : function() {
			if( !this.checkFormValid() ){
				return;
			}
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);
			
			if(form.autoSync){
				ctrl.set("form.autoSync", "1");
			}else{
				ctrl.set("form.autoSync", "0");
			};
			
			if(form.apmServerEnable){
				ctrl.set("form.apmServerEnable", "1");
			}else{
				ctrl.set("form.apmServerEnable", "0");
			};
			
			this.store.ajax("POST", "configs/systemConfig", {
				machineName : form.machineName,
				localIp : form.localIp,
				subnetMask : form.subnetMask,
				gateway : form.gateway,
				firstDNS : form.firstDNS,
				secondDNS : form.secondDNS,
				LocalTime : form.LocalTime,
				systemDate : form.systemDate,
				systemTime : form.systemTime,
				autoSync :form.autoSync,
				webPort : form.webPort,
				apmServerEnable :form.apmServerEnable,
				remoteIp : form.remoteIp,
				timeServer : form.timeServer,
				port : form.port,
				confType:form.confType
			}).then(function(response) {
				//todo: 保存结果返回逻辑
				if(response.status=="success"){
					ctrl.set("saveResult", "保存成功！");
				}
			}, function() {
				ctrl.set("saveResult", "保存异常！");
			});
		},

		keepSync : function() {
			alert("立即同步");
			var ctrl = this;
			this.store.ajax("POST", "configs/systemConfig", {
				
				
			});
			
		}
	}
});